package com.fw.ui.view.event;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.fw.ui.bao.MessageBAO;
import com.fw.ui.view.common.TablePanel;
import com.vaadin.server.Page;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Notification;

public abstract class AbstractSortTableButtonClickListener extends AbstractListener implements ClickListener {

    private static final long serialVersionUID = 1L;

    private TablePanel tablePanel;

    @Autowired
    private MessageBAO messageBAO;

    @Override
    public void buttonClick(ClickEvent event) {
	Logger log = LoggerFactory.getLogger(this.getClass());
	try {
	    if (this.filter()) {
		event.getButton().setEnabled(true);
		return;
	    }

	    Integer size = tablePanel.getManualPageSize();
	    int page = 1;

	    String sort1 = (String) tablePanel.getSortingTablePanel().getCbbSelectSort1().getValue();
	    String sort2 = (String) tablePanel.getSortingTablePanel().getCbbSelectSort2().getValue();
	    String sort3 = (String) tablePanel.getSortingTablePanel().getCbbSelectSort3().getValue();
	    String sortType1 = (String) tablePanel.getSortingTablePanel().getOpgSortingType1().getValue();
	    String sortType2 = (String) tablePanel.getSortingTablePanel().getOpgSortingType2().getValue();
	    String sortType3 = (String) tablePanel.getSortingTablePanel().getOpgSortingType3().getValue();

	    Map<String, String> sortMap = new LinkedHashMap<String, String>();
	    if (sort1 != null) {
		sortMap.put(sort1, sortType1);
	    }
	    if (sort2 != null && !sortMap.containsKey(sort2)) {
		sortMap.put(sort2, sortType2);
	    }
	    if (sort3 != null && !sortMap.containsKey(sort3)) {
		sortMap.put(sort3, sortType3);
	    }

	    if (this.validate(event, page, size.intValue(), sortMap)) {
		log.info("VALIDATE EVENT CLICK LISTENER [RESULT:TRUE]");
		log.info("EXECUTE EVENT CLICK LISTENER START");
		this.click(event, page, size.intValue(), sortMap);
		log.info("EXECUTE EVENT CLICK LISTENER FINISH");
	    } else {
		log.info("VALIDATE EVENT CLICK LISTENER [RESULT:FALSE]");
	    }
	} catch (TimeoutException e) {
	    log.error("EVENT CLICK LISTENER ERROR [" + e.getMessage() + "]");
	    e.printStackTrace();
	    String msg = this.messageBAO.getMessage("message.error.timeout");
	    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
	    notification.setDelayMsec(1000);
	    notification.show(Page.getCurrent());
	} catch (Exception e) {
	    log.error("EVENT CLICK LISTENER ERROR [" + e.getMessage() + "]");
	    e.printStackTrace();
	    String msg = this.messageBAO.getMessage("message.error.common");
	    Notification notification = new Notification("Warning", msg, Notification.Type.WARNING_MESSAGE, true);
	    notification.setDelayMsec(1000);
	    notification.show(Page.getCurrent());
	}
	event.getButton().setEnabled(true);
    }

    public abstract boolean validate(ClickEvent event, int page, int size, Map<String, String> sortMap) throws Exception;

    public abstract void click(ClickEvent event, int page, int size, Map<String, String> sortMap) throws Exception;

    public TablePanel getTablePanel() {
	return tablePanel;
    }

    public void setTablePanel(TablePanel tablePanel) {
	this.tablePanel = tablePanel;
    }

}
